import {Component, EventEmitter, Input, OnDestroy, OnInit, Output} from '@angular/core';
import {BarnProject} from "../../../welcome/business/project/entity/barn-project";
import {ProjectService} from "../../../welcome/business/project/service/project.service";
import {ProjectProductService} from "../../../welcome/business/service/project-product.service";
import {Constants} from "../../../../tools";
import {Subject, Subscription} from "rxjs";
import {debounceTime} from "rxjs/operators";

@Component({
  selector: 'app-project-selection',
  templateUrl: './project-selection.component.html',
  styleUrls: ['./project-selection.component.less']
})
export class ProjectSelectionComponent implements OnInit, OnDestroy {

  // product id
  @Input()
  productId: string

  @Output()
  cancel = new EventEmitter()

  @Output()
  success = new EventEmitter()

  /**
   * can not selected projects' id
   */
  @Input()
  disable: string[]

  // query condition
  _filter: string = ''
  finalFilterValue = ''

  filter$ = new Subject<string>()
  filterObserve = this.filter$.asObservable().pipe(debounceTime(500))
  filterSubscription: Subscription

  projects: BarnProject[]
  // when product id not empty ,this sourceProject present all data of project
  sourceProject: BarnProject[]
  total = 1000
  current = 1;
  pageSize = 10

  loading = false;

  selectedProject: BarnProject;

  constructor(private projectService: ProjectService, private ppService: ProjectProductService) {

  }

  ngOnInit(): void {
    // if product id was gaven ,get all product related project ,otherwise query all enabled project
    if (this.productId) {
      this.queryByProduct()
    } else {
      this.queryAll();
    }

    this.filterSubscription = this.filterObserve.subscribe(value => {
      this.finalFilterValue = value
      if (this.productId) {
        this.loading = false
        if (value) {
          this.projects = this.sourceProject.filter(v => v.projectName.indexOf(value) >= 0);
        } else {
          this.projects = this.sourceProject
        }
      } else {
        this.queryAll()
      }
    })
  }

  queryAll() {
    const param = new BarnProject();
    param.enable = Constants.Enable.YES;
    param.projectName = this.finalFilterValue
    this.loading = true
    this.projectService.query(param, this.current, this.pageSize).subscribe(value => {
      this.projects = value.data;
      this.current = value.current;
      this.total = value.count;
      this.loading = false
    }, () => this.loading = false);
  }

  queryByProduct() {
    this.loading = true;
    this.ppService.getProjectByProduct(this.productId).subscribe(value => {
      this.projects = value
      this.sourceProject = value
      this.loading = false
    }, () => this.loading = false)
  }

  selectProject(project: BarnProject) {
    if (this.isDisable(project.pid)) {
      return;
    }

    if (!this.disable && !this.disable.length) {
      const dis = this.disable.find(value => value === project.pid);
      if (dis) {
        return
      }
    }

    if (this.selectedProject == null || this.selectedProject.pid !== project.pid) {
      this.selectedProject = project;
    } else {
      this.selectedProject = null;
    }
  }

  isDisable(pid): boolean {
    if (this.disable && this.disable.length) {
      const dis = this.disable.find(value => value === pid);
      return !!dis;
    } else {
      return false
    }

  }

  cancelEvent() {
    this.cancel.emit()
  }

  selectedSuccess() {
    this.success.emit(this.selectedProject)
  }

  set filter(v) {
    this._filter = v;
    this.loading = true;
    this.filter$.next(this._filter);
  }

  get filter() {
    return this._filter
  }

  ngOnDestroy(): void {
    if (this.filterSubscription) {
      this.filterSubscription.unsubscribe()
    }
  }

}
